package com.mars.diancan.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 *
 * 手动微信授权
 * @author MARS
 * @date 2018/6/28
 */
@RestController
@RequestMapping("/weixin")
@Slf4j
public class WexinController {

    /**
     * 微信授权 测试账号
     * 1、获取code
     * https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx2eb4b262e62ca56e
     *       &redirect_uri=http://diancan.nat100.top/diancan/weixin/auth
     *       &response_type=code&scope=snsapi_base
     *       &state=STATE#wechat_redirect
     *
     * 2、获取授权
     * @param code
     */
    @RequestMapping("/auth")
    public void auth(@RequestParam("code")String code) {
        log.info("进入auth方法");
        log.info("code={}",code);

        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx2eb4b262e62ca56e&secret=a1eff6827ee4031559506928985f48cc&code="+code+"&grant_type=authorization_code";

        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.getForObject(url,String.class);
        log.info("response={}",response);
    }
}
